/**
 * Copyright (c) 2024 wangshuangbao personal. All rights reserved.
 */
package com.wang.aspect;

import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;

import java.lang.reflect.Parameter;
import java.util.HashMap;

/**
 * @Author wsb
 * @Description 方法执行AOP
 */
@Slf4j
@Aspect
@Component
public class FunctionAspect {

    @Pointcut("execution(* com.wang.*.*(..))")
    public void pointcut() {
    }

    @Around("pointcut()")
    public Object around(ProceedingJoinPoint pjp) {
        Object result = null;
        MethodSignature signature = (MethodSignature) pjp.getSignature();
        Parameter[] parameters = signature.getMethod().getParameters();
        HashMap<String, String> paramMap = new HashMap<>();
        for (int i = 0; i < parameters.length; i++) {
            String paramName = parameters[i].getName();
            Object paramValue = pjp.getArgs()[i];
            paramMap.put(paramName, paramValue.toString());
        }
        // 市场代码
        String marketCode = paramMap.get("marketCode");
        log.info("方法【{}】传递的市场代码为：【{}】", signature.getMethod().getName(), marketCode);
        try {
            result = pjp.proceed();
        } catch (Throwable throwable) {
            log.error("方法执行AOP执行方法异常", throwable);
        }
        return result;
    }
}
